//
//  PTSBaseTabBar.m
//  PTS80
//
//  Created by 黄杰 on 15/11/3.
//  Copyright © 2015年 黄杰. All rights reserved.
//

#import "PTSBaseTabBar.h"
#import "PTSTabBarButton.h"

@interface PTSBaseTabBar ()
@property (nonatomic, weak) UIView *line;
@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, weak) PTSTabBarButton *selectedBtn;
@property (nonatomic, strong) UIColor *itemColor;
@property (nonatomic, assign) BOOL hasLayout;
@end

@implementation PTSBaseTabBar

- (NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        self.backgroundColor = [UIColor whiteColor];
        
        // 上部分蓝色横线
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor blackColor];
        [self addSubview:line];
        self.line = line;
    }
    return self;
}

- (void)setupSelectedItemColor:(UIColor *)itemColor
{
    self.itemColor = itemColor;
}

- (void)setupLineColor:(UIColor *)lineColor
{
    self.line.backgroundColor = lineColor;
}

- (void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    // 添加Item
    PTSTabBarButton *btn = [PTSTabBarButton buttonWithType:UIButtonTypeCustom];
    btn.tag = self.btnArray.count;
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.imageView.contentMode = UIViewContentModeCenter;
    [btn setImage:item.image forState:UIControlStateNormal];
    [btn setImage:item.selectedImage forState:UIControlStateSelected];
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchDown];
    [btn setTitleColor:self.titleColor forState:UIControlStateNormal];
    [btn setTitleColor:self.selectedColor forState:UIControlStateSelected];
    btn.itemColor = self.itemColor;
    btn.item = item;
    if (btn.tag == 0) {
        [self didClickBtn:btn];
    }
    [self addSubview:btn];
    [self.btnArray addObject:btn];
    
}

- (void)didSelectedItemWithTag:(NSInteger)tag
{
    PTSTabBarButton *btn = self.btnArray[tag];
    [self didClickBtn:btn];
}

#pragma mark - 点击了按钮
- (void)didClickBtn:(PTSTabBarButton *)btn
{
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    
    if ([self.delegate respondsToSelector:@selector(baseTabBar:getItemTag:)]) {
        [self.delegate baseTabBar:self getItemTag:btn.tag];
    }
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (!self.hasLayout) {
        CGFloat lineX = 0;
        CGFloat lineY = 0;
        CGFloat lineW = self.frame.size.width;
        CGFloat lineH = 0.1;
        self.line.frame = CGRectMake(lineX, lineY, lineW, lineH);
        
        if (self.btnArray.count) {
            CGFloat btnW = self.frame.size.width / self.btnArray.count;
            CGFloat btnH = self.frame.size.height - lineH;
            CGFloat btnY = lineH;
            for (NSInteger i = 0; i < self.btnArray.count; i++) {
                PTSTabBarButton *btn = self.btnArray[i];
                CGFloat btnX = i * btnW;
                btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            }
        }
        self.hasLayout = YES;
    }

}

@end
